//
//  ListController.m
//  SeeFood
//
//  Created by 陈伟捷 on 15/11/24.
//  Copyright © 2015年 纪洪波. All rights reserved.
//

#import "ListController.h"
#import "ListCell.h"
#import "DetailListController.h"
#import "ListModel.h"

#define KScreenWidth [[UIScreen mainScreen] bounds].size.width
#define KScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ListController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)NSMutableArray *modelArray;

@property(nonatomic, strong)UICollectionView *collectionView;

@end

@implementation ListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 设置边距
    layout.minimumInteritemSpacing = 3;
    layout.minimumLineSpacing = 3;
    CGFloat width = (KScreenWidth - 3) / 2;
    
    layout.itemSize = CGSizeMake(width, width);
    
    // 创建一个集合视图
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[ListCell class] forCellWithReuseIdentifier:@"classifiedCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:@"http://apis.juhe.cn/cook/category?key=parentid=&dtype=&key=b38444bae8b8b53d67ae80cf4b81c9d5"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSArray *resultArray = dic[@"result"];
        
        NSDictionary *listDic = resultArray[1];
        
        NSArray *listArray = listDic[@"list"];
        
        self.modelArray = [NSMutableArray array];
        
        for (int i = 0; i < 15; i++) {
            
            NSDictionary *dic = listArray[i];
            
            ListModel *model = [[ListModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.modelArray addObject:model];
        }
        // 回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    }] resume];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.modelArray != nil) {
        return self.modelArray.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"classifiedCell" forIndexPath:indexPath];
    
    ListModel *model = self.modelArray[indexPath.row];
    
    [cell setImageViewWithImageName:[NSString stringWithFormat:@"%02ld", indexPath.row]];
    
    cell.nameLabel.text = model.name;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailListController *detailListController = [[DetailListController alloc]init];
    
    ListModel *model = self.modelArray[indexPath.row];
    
    detailListController.model = model;
    
    [self.navigationController pushViewController:detailListController animated:YES];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)initUI {
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.945 green:0.263 blue:0.255 alpha:1.000]];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}

@end
